package com.meyoung.day2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WindowsSelectTest {
    WebDriver driver;
    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\cll520\\IdeaProjects\\selenium11\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
    }
    /*
    打开我们的测试页面
    点击open new Windows
    在新的窗口点击baidu
     */
    @Test
    public void selectTest() throws InterruptedException {
        //打开网站
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        //定位open new Windows
        driver.findElement(By.linkText("Open new window")).click();
        Thread.sleep(2000);
        //获取当前页面的句柄
        String handle1 = driver.getWindowHandle();
        //使用for循环获取点击后的页面的句柄，并且转交操作
        for (String handles :driver.getWindowHandles()){
            if (handles.equals(handle1))
                continue;
            driver.switchTo().window(handles);
        }
        //定位baidu，并点击
        driver.findElement(By.linkText("baidu")).click();
        Thread.sleep(2000);
    }
    @AfterMethod
    public void closedBrowser(){
        driver.quit();
    }
}
